package com.ics.top.batch.management.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.*;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
 * Created by Administrator on 2018/4/25.
 */
@Service
public class BatchUserManagementClient {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private JobLauncher jobLauncher;
    @Autowired
    @Qualifier("userManagementJob1")
    private Job job;

    @Scheduled(cron="0 30 2 * * ?") //10分钟执行一次,把拨打计划已完成的数据，同步到用户管理表去
//    @Scheduled(cron="0/10 * * * * ?") //10分钟执行一次,把拨打计划已完成的数据，同步到用户管理表去
    public void batchStart() throws Exception {
        log.info("执行了userManagementJob1定时器");
        try {
            JobParameters jobParameters =
                new JobParametersBuilder()
                    .addLong("time",System.currentTimeMillis()).toJobParameters(); // 采用这个方法，会从新执行job，添加一个新的识别长参数。
            JobExecution execution = jobLauncher.run(job, jobParameters);
            System.out.println("Execution status: "+ execution.getStatus());
        } catch (JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException
            | JobParametersInvalidException e) {
            e.printStackTrace();
        }
    }
}
